
/*
假设头像是72高度，一个屏幕则渲染
240*72
240*72*2/1024 = 33.75KB(0x8700)
*/


#include <stdint.h>
#include "../gba_dma.h"
#include "../fe8.h"
#include "mareeta.h"
#include "../gba_hack.h"

#define draw_pal evel_pal
#define draw_tile evel_tile
#define linepal evel_linepal
//需要临时设置调色板开始地址
#undef color_start
#include "evel.h"
#undef draw_pal     //evel_pal
#undef draw_tile    //evel_tile
#undef linepal      //evel_linepal

#define draw_pal mareeta_evel_pal
#define draw_tile mareeta_evel_tile
#define linepal mareeta_evel_linepal
//需要临时设置调色板开始地址
#undef color_start
#include "mareeta_evel.h"
#undef draw_pal     //evel_pal
#undef draw_tile    //evel_tile
#undef linepal      //evel_linepal

#define PPAL_OFFSET 128

#define draw_pal lin_pal
#define draw_tile lin_tile
#define linepal lin_linepal
//需要临时设置调色板开始地址
#define color_start 0
#include "evel98x72.h"
#undef draw_pal     //evel_pal
#undef draw_tile    //evel_tile
#undef linepal      //evel_linepal

#define COLOR_SET
#define color_start PPAL_OFFSET*1
#define draw_tile lin_tile2
#include "evel98x72.h"
#undef color_start
#undef COLOR_SET
#undef draw_tile



#define draw_pal evel98_pal
#define draw_tile evel98_tile
#define linepal evel98_linepal
//需要临时设置调色板开始地址
#undef color_start
//#include "evel98x72.h"
#include "mareeta98.h"
#undef draw_pal     //evel_pal
#undef draw_tile    //evel_tile
#undef linepal      //evel_linepal


#define COLOR_SET
#define color_start PPAL_OFFSET*1
#define draw_tile evel_tile1
#include "evel.h"
#undef color_start
#undef draw_tile

#define color_start PPAL_OFFSET*1
#define draw_tile evel_tile2
#include "evel.h"
#undef color_start
#undef draw_tile

#define color_start PPAL_OFFSET*1
#define draw_tile evel_tile3
#include "evel.h"
#undef color_start
#undef draw_tile

#define color_start PPAL_OFFSET*1
#define draw_tile mareet_tile1
#include "mareeta.h"
#undef color_start
#undef draw_tile

#define color_start PPAL_OFFSET*1
#define draw_tile mareet_tile2
#include "mareeta.h"
#undef color_start
#undef draw_tile

#define color_start PPAL_OFFSET*1
#define draw_tile mareet_tile3
#include "mareeta.h"
#undef color_start
#undef draw_tile

#include "../hack/portrait_def.h"


const uint8_t size_64x80[] = {
    64,80,
};

const uint8_t size_120x80[] = {
    120,80,
};

const uint8_t size_104x80[] = {
    120,80
};

const uint8_t size_128x80[] = {
    128,80,
};

typedef struct{
    uint8_t     *size;
    void*       palbase;
    uint8_t*    tile[4];
    uint8_t**   draw_linepal;
}new_portrait_t;

const new_portrait_t new_portrait[0x100] = {
    //[0x6b] = {size_64x80,draw_pal,{draw_tile,mareet_tile1,mareet_tile2,mareet_tile3},linepal},
//[0x6c] = {size_64x80,evel_pal,evel_tile,evel_linepal},

#if 0
    //双头像
    [0x6b] = {size_120x80,mareeta_evel_pal,{mareeta_evel_tile,mareeta_evel_tile,mareeta_evel_tile,mareeta_evel_tile},mareeta_evel_linepal},
#else
    [0x4] = {size_104x80,evel98_pal,{evel98_tile,evel98_tile,evel98_tile,evel98_tile},evel98_linepal},
#endif

#if 0
    [0x2c] = {size_64x80,evel_pal,{evel_tile,evel_tile1,evel_tile2,evel_tile3},evel_linepal},
#else
    //双头像
    //[0x2] = {size_120x80,lin_pal,{lin_tile,lin_tile2,lin_tile,lin_tile},lin_linepal},
    //[0x4] = {size_120x80,lin_pal,{lin_tile,lin_tile2,lin_tile,lin_tile},lin_linepal},
    //[0x4] = {size_104x80,SYM_1_draw_pal,{SYM_1_tile,SYM_1_tile2,SYM_1_tile,SYM_1_tile},SYM_1_linepal,
    //},

    #include "../hack/portrait_set.h"
#endif
};


#define nasm(s) asm(s); asm("\n");

#define oam_att0_color256       (1<<13)
#define oam_att0_square         (0<<14)
#define oam_att0_horizon        (1<<14)
#define oam_att0_vertical       (2<<14)
#define oam_att0_prohibited     (3<<14)
#define oam_att0_mosaic         (1<<12)

#define oam_att1_hflip          (1<<12)
#define oam_att1_vflip          (1<<13)
#define oam_att1_size8          (0<<14)
#define oam_att1_size16          (1<<14)
#define oam_att1_size32          (2<<14)
#define oam_att1_size64          (3<<14)

#define oam_att2_priority(v)    ((v)<<10)
#define oam_att2_color16(v)     ((v)<<12)

const uint16_t oam_att[] = {
    //长度
    //(sizeof(oam_att)>>2),
    #if 0
    1,
    oam_att0_square|oam_att0_color256, //正方形256色
    oam_att1_size64/*|oam_att1_hflip*/|0x1e0,
    0,
    #else

    #if 0
    //以32x16显示
    8,
    oam_att0_horizon|oam_att0_color256, //正方形256色
    oam_att1_size32/*|oam_att1_hflip*/|0x1e0,
    0,
    oam_att0_horizon|oam_att0_color256, //正方形256色
    oam_att1_size32/*|oam_att1_hflip*/|0x1e0+32,
    8,
    oam_att0_horizon|oam_att0_color256 + 1*16, //正方形256色
    oam_att1_size32/*|oam_att1_hflip*/|0x1e0,
    1*0x40+0,
    oam_att0_horizon|oam_att0_color256 + 1*16, //正方形256色
    oam_att1_size32/*|oam_att1_hflip*/|0x1e0+32,
    1*0x40+8,
    oam_att0_horizon|oam_att0_color256 + 2*16, //正方形256色
    oam_att1_size32/*|oam_att1_hflip*/|0x1e0,
    2*0x40+0,
    oam_att0_horizon|oam_att0_color256 + 2*16, //正方形256色
    oam_att1_size32/*|oam_att1_hflip*/|0x1e0+32,
    2*0x40+8,
    oam_att0_horizon|oam_att0_color256 + 3*16, //正方形256色
    oam_att1_size32/*|oam_att1_hflip*/|0x1e0,
    3*0x40+0,
    oam_att0_horizon|oam_att0_color256 + 3*16, //正方形256色
    oam_att1_size32/*|oam_att1_hflip*/|0x1e0+32,
    3*0x40+8,
    #elif 1
    //10 + 5,
    10,
    oam_att0_horizon|oam_att0_color256, //正方形256色
    oam_att1_size32/*|oam_att1_hflip*/|0x1e0,
    0,
    oam_att0_horizon|oam_att0_color256, //正方形256色
    oam_att1_size32/*|oam_att1_hflip*/|0x1e0+32,
    8,
    oam_att0_horizon|oam_att0_color256 + 1*16, //正方形256色
    oam_att1_size32/*|oam_att1_hflip*/|0x1e0,
    0*0x40+0,
    oam_att0_horizon|oam_att0_color256 + 1*16, //正方形256色
    oam_att1_size32/*|oam_att1_hflip*/|0x1e0+32,
    0*0x40+8,
    oam_att0_horizon|oam_att0_color256 + 2*16, //正方形256色
    oam_att1_size32/*|oam_att1_hflip*/|0x1e0,
    0*0x40+0,
    oam_att0_horizon|oam_att0_color256 + 2*16, //正方形256色
    oam_att1_size32/*|oam_att1_hflip*/|0x1e0+32,
    0*0x40+8,
    oam_att0_horizon|oam_att0_color256 + 3*16, //正方形256色
    oam_att1_size32/*|oam_att1_hflip*/|0x1e0,
    0*0x40+0,
    oam_att0_horizon|oam_att0_color256 + 3*16, //正方形256色
    oam_att1_size32/*|oam_att1_hflip*/|0x1e0+32,
    0*0x40+8,

    oam_att0_horizon|oam_att0_color256 + 4*16, //正方形256色
    oam_att1_size32/*|oam_att1_hflip*/|0x1e0,
    0*0x40+0,
    oam_att0_horizon|oam_att0_color256 + 4*16, //正方形256色
    oam_att1_size32/*|oam_att1_hflip*/|0x1e0+32,
    0*0x40+8,

    #if 1
    oam_att0_horizon|oam_att0_color256, //正方形256色
    oam_att1_size32/*|oam_att1_hflip*/|0x1e0+32+32,
    16,
    oam_att0_horizon|oam_att0_color256 + 1*16, //正方形256色
    oam_att1_size32/*|oam_att1_hflip*/|0x1e0+32+32,
    16,
    oam_att0_horizon|oam_att0_color256 + 2*16, //正方形256色
    oam_att1_size32/*|oam_att1_hflip*/|0x1e0+32+32,
    16,
    oam_att0_horizon|oam_att0_color256 + 3*16, //正方形256色
    oam_att1_size32/*|oam_att1_hflip*/|0x1e0+32+32,
    16,
    oam_att0_horizon|oam_att0_color256 + 4*16, //正方形256色
    oam_att1_size32/*|oam_att1_hflip*/|0x1e0+32+32,
    16,
    #endif


    #elif 0
        //以32x16显示
    8,
    oam_att0_horizon|oam_att0_color256, //正方形256色
    oam_att1_size32/*|oam_att1_hflip*/|0x1e0,
    0,
    oam_att0_horizon|oam_att0_color256, //正方形256色
    oam_att1_size32/*|oam_att1_hflip*/|0x1e0+32,
    8,
    oam_att0_horizon|oam_att0_color256 + 1*16, //正方形256色
    oam_att1_size32/*|oam_att1_hflip*/|0x1e0,
    16,
    oam_att0_horizon|oam_att0_color256 + 1*16, //正方形256色
    oam_att1_size32/*|oam_att1_hflip*/|0x1e0+32,
    24,
    oam_att0_horizon|oam_att0_color256 + 2*16, //正方形256色
    oam_att1_size32/*|oam_att1_hflip*/|0x1e0,
    0x40+0,
    oam_att0_horizon|oam_att0_color256 + 2*16, //正方形256色
    oam_att1_size32/*|oam_att1_hflip*/|0x1e0+32,
    0x40+8,
    oam_att0_horizon|oam_att0_color256 + 3*16, //正方形256色
    oam_att1_size32/*|oam_att1_hflip*/|0x1e0,
    0x40+16,
    oam_att0_horizon|oam_att0_color256 + 3*16, //正方形256色
    oam_att1_size32/*|oam_att1_hflip*/|0x1e0+32,
    0x40+24,
    #elif 1
    //应该直接使用以 01,02,03,04,05,06,07,08 ... 排序，遇到00时，主要把颜色置为00
    #endif


    #endif
};

const uint16_t oam_att_120x80[] = {
    15,
    oam_att0_horizon|oam_att0_color256, //正方形256色
    oam_att1_size32/*|oam_att1_hflip*/|0x1e0 - 16,
    0,
    oam_att0_horizon|oam_att0_color256, //正方形256色
    oam_att1_size32/*|oam_att1_hflip*/|0x1e0+32 - 16,
    8,
    oam_att0_horizon|oam_att0_color256 + 1*16, //正方形256色
    oam_att1_size32/*|oam_att1_hflip*/|0x1e0 - 16,
    0*0x40+0,
    oam_att0_horizon|oam_att0_color256 + 1*16, //正方形256色
    oam_att1_size32/*|oam_att1_hflip*/|0x1e0+32- 16,
    0*0x40+8,
    oam_att0_horizon|oam_att0_color256 + 2*16, //正方形256色
    oam_att1_size32/*|oam_att1_hflip*/|0x1e0- 16,
    0*0x40+0,
    oam_att0_horizon|oam_att0_color256 + 2*16, //正方形256色
    oam_att1_size32/*|oam_att1_hflip*/|0x1e0+32- 16,
    0*0x40+8,
    oam_att0_horizon|oam_att0_color256 + 3*16, //正方形256色
    oam_att1_size32/*|oam_att1_hflip*/|0x1e0- 16,
    0*0x40+0,
    oam_att0_horizon|oam_att0_color256 + 3*16, //正方形256色
    oam_att1_size32/*|oam_att1_hflip*/|0x1e0+32- 16,
    0*0x40+8,

    oam_att0_horizon|oam_att0_color256 + 4*16, //正方形256色
    oam_att1_size32/*|oam_att1_hflip*/|0x1e0- 16,
    0*0x40+0,
    oam_att0_horizon|oam_att0_color256 + 4*16, //正方形256色
    oam_att1_size32/*|oam_att1_hflip*/|0x1e0+32- 16,
    0*0x40+8,

    #if 1
    oam_att0_horizon|oam_att0_color256, //正方形256色
    oam_att1_size32/*|oam_att1_hflip*/|0x1e0+32+32- 16,
    16,
    oam_att0_horizon|oam_att0_color256 + 1*16, //正方形256色
    oam_att1_size32/*|oam_att1_hflip*/|0x1e0+32+32- 16,
    16,
    oam_att0_horizon|oam_att0_color256 + 2*16, //正方形256色
    oam_att1_size32/*|oam_att1_hflip*/|0x1e0+32+32- 16,
    16,
    oam_att0_horizon|oam_att0_color256 + 3*16, //正方形256色
    oam_att1_size32/*|oam_att1_hflip*/|0x1e0+32+32- 16,
    16,
    oam_att0_horizon|oam_att0_color256 + 4*16, //正方形256色
    oam_att1_size32/*|oam_att1_hflip*/|0x1e0+32+32 - 16,
    16,
    #endif
};

// const uint16_t oam_att_120x80[] = {
//     15,
//     oam_att0_horizon|oam_att0_color256, //正方形256色
//     oam_att1_size32/*|oam_att1_hflip*/|0x1e0,
//     0,
//     oam_att0_horizon|oam_att0_color256, //正方形256色
//     oam_att1_size32/*|oam_att1_hflip*/|0x1e0+32,
//     8,
//     oam_att0_horizon|oam_att0_color256 + 1*16, //正方形256色
//     oam_att1_size32/*|oam_att1_hflip*/|0x1e0,
//     0*0x40+0,
//     oam_att0_horizon|oam_att0_color256 + 1*16, //正方形256色
//     oam_att1_size32/*|oam_att1_hflip*/|0x1e0+32,
//     0*0x40+8,
//     oam_att0_horizon|oam_att0_color256 + 2*16, //正方形256色
//     oam_att1_size32/*|oam_att1_hflip*/|0x1e0,
//     0*0x40+0,
//     oam_att0_horizon|oam_att0_color256 + 2*16, //正方形256色
//     oam_att1_size32/*|oam_att1_hflip*/|0x1e0+32,
//     0*0x40+8,
//     oam_att0_horizon|oam_att0_color256 + 3*16, //正方形256色
//     oam_att1_size32/*|oam_att1_hflip*/|0x1e0,
//     0*0x40+0,
//     oam_att0_horizon|oam_att0_color256 + 3*16, //正方形256色
//     oam_att1_size32/*|oam_att1_hflip*/|0x1e0+32,
//     0*0x40+8,

//     oam_att0_horizon|oam_att0_color256 + 4*16, //正方形256色
//     oam_att1_size32/*|oam_att1_hflip*/|0x1e0,
//     0*0x40+0,
//     oam_att0_horizon|oam_att0_color256 + 4*16, //正方形256色
//     oam_att1_size32/*|oam_att1_hflip*/|0x1e0+32,
//     0*0x40+8,

//     #if 1
//     oam_att0_horizon|oam_att0_color256, //正方形256色
//     oam_att1_size32/*|oam_att1_hflip*/|0x1e0+32+32,
//     16,
//     oam_att0_horizon|oam_att0_color256 + 1*16, //正方形256色
//     oam_att1_size32/*|oam_att1_hflip*/|0x1e0+32+32,
//     16,
//     oam_att0_horizon|oam_att0_color256 + 2*16, //正方形256色
//     oam_att1_size32/*|oam_att1_hflip*/|0x1e0+32+32,
//     16,
//     oam_att0_horizon|oam_att0_color256 + 3*16, //正方形256色
//     oam_att1_size32/*|oam_att1_hflip*/|0x1e0+32+32,
//     16,
//     oam_att0_horizon|oam_att0_color256 + 4*16, //正方形256色
//     oam_att1_size32/*|oam_att1_hflip*/|0x1e0+32+32,
//     16,
//     #endif
// };

typedef struct{
    uint8_t     *tile;
    uint8_t     *dest_vram;
    int8_t      tile_width;
}hblank_draw_t;

typedef struct{
    uint32_t    pal_addr;
    uint32_t    pal_src;
    //uint32_t    func;
    int16_t     pal_num;
    uint32_t     *dma;
}hblank_draw_line_t;

enum{
    show_none,      //默认
    show_menu = 2,      //菜单显示人物
    show_text,      //对话显示人物
};

typedef struct{
    int16_t         count;
    uint16_t    hblank_count;
    uint32_t    event;
    uint16_t    start_line;
    uint16_t    init_tile_line;
    uint8_t     status;
    uint8_t     show_type;
    int16_t     wait;
    uint8_t id[3];
    hblank_draw_t       draw[4];
    uint8_t **text_ptr;
    uint8_t *cur_text;
    int8_t lineadd;
    uint8_t line0;
    uint8_t line1;
    int16_t             initd;
    new_portrait_t      *portrait[4];
    hblank_draw_line_t  lines[80];
    uint8_t             exec[0];
}hblank_funcproc;

hblank_funcproc * const hblank_proc = (void*)0x201df00;

// #define _0x3000a00 0x3000a00
// #define _0x3000b30 0x3000b30

#define _0x3000a00 0x3006a00
#define _0x3000b30 (_0x3000a00+0x140)

extern void pal_copy();
extern void pal_copy_end();
extern void pal_copy128(uint32_t dest,uint32_t src);
extern void pal_copyBX(uint32_t dest,uint32_t src);
extern void pal_copy120BX(uint32_t dest,uint32_t src);
extern void tile_copy(uint32_t dest,uint32_t src);
extern int funcoffset120;
static void hblank();
extern void hblank_end();
static void init_RAM(){
    uint32_t begin = (uint32_t)pal_copy;
    uint32_t end = (uint32_t)pal_copy_end;
    gba_memcpy(_0x3000a00,begin,end-begin);
    gba_memcpy(_0x3000b30,hblank,0x1e0+0x40+0x40);
};

void pal_copyram(){
    nasm("pal_copyBX:");
    nasm("ldr r2,PAL_ADDR");
    nasm("bx r2");
    nasm("PAL_ADDR:.int 0x03000a00");
    nasm(".arm");
    nasm("pal_copy:");
    nasm("push {r4,r5,r6,r7,r8,r9,r10,r11,r12,lr}");
    //0
    nasm("ldmia r1!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    nasm("stmia r0!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    //1
    nasm("ldmia r1!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    nasm("stmia r0!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    //2
    nasm("ldmia r1!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    nasm("stmia r0!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    //3
    nasm("ldmia r1!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    nasm("stmia r0!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    //4
    nasm("ldmia r1!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    nasm("stmia r0!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    //还剩余16字节
    nasm("ldmia r1!,{r2,r3,r4,r5}");
    nasm("stmia r0!,{r2,r3,r4,r5}");
    //
    nasm("pop {r4,r5,r6,r7,r8,r9,r10,r11,r12,lr}");
    nasm("bx lr");

    nasm("pal_copy120:");
    nasm("push {r4,r5,r6,r7,r8,r9,r10,r11,r12,lr}");
    //0
    nasm("ldmia r1!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    nasm("stmia r0!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    //1
    nasm("ldmia r1!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    nasm("stmia r0!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    //2
    //还剩余24字节
    nasm("ldmia r1!,{r2,r3,r4,r5,r6,r7}");
    nasm("stmia r0!,{r2,r3,r4,r5,r6,r7}");
    //
    nasm("pop {r4,r5,r6,r7,r8,r9,r10,r11,r12,lr}");
    nasm("bx lr");
    nasm("tile_copy:");

    nasm("push {r4,r5,r6,r7,r8,r9,r10,r11,r12,lr}");
    //如果是120宽度的，15个tile，一个tile，68字节,需要复制960字节,
    //0
    nasm("ldmia r1!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    nasm("stmia r0!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    //1
    nasm("ldmia r1!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    nasm("stmia r0!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    //2
    nasm("ldmia r1!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    nasm("stmia r0!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    //3
    nasm("ldmia r1!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    nasm("stmia r0!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    //4
    nasm("ldmia r1!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    nasm("stmia r0!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    //5
    nasm("ldmia r1!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    nasm("stmia r0!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    //6
    nasm("ldmia r1!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    nasm("stmia r0!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    //7
    nasm("ldmia r1!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    nasm("stmia r0!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    //8
    nasm("ldmia r1!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    nasm("stmia r0!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    //9
    nasm("ldmia r1!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    nasm("stmia r0!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");

    //10
    //0
    nasm("ldmia r1!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    nasm("stmia r0!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    //1
    nasm("ldmia r1!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    nasm("stmia r0!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    //2
    nasm("ldmia r1!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    nasm("stmia r0!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    //3
    nasm("ldmia r1!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    nasm("stmia r0!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    //4
    nasm("ldmia r1!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    nasm("stmia r0!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    //5
    nasm("ldmia r1!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    nasm("stmia r0!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    //6
    nasm("ldmia r1!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    nasm("stmia r0!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    //7
    nasm("ldmia r1!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    nasm("stmia r0!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    //8
    nasm("ldmia r1!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    nasm("stmia r0!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    //9
    nasm("ldmia r1!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");
    nasm("stmia r0!,{r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r14}");

    nasm("pop {r4,r5,r6,r7,r8,r9,r10,r11,r12,lr}");
    nasm("bx lr");
    nasm("pal_copy_end:");

    nasm(".thumb");
    nasm("funcoffset120:");
    nasm(".int pal_copy120-pal_copy");
    nasm("pal_copy120BX:");
    nasm("ldr r2,PAL_COPY120BX_ADDR");
    nasm("bx r2");
    nasm("PAL_COPY120BX_ADDR:.int 0x03000a00 + (pal_copy120-pal_copy)");
}

inline static void hblank_draw_tile_wram(hblank_draw_t *d,int varmy,int tiley){
    #if 0
    //#define gba_memcpy(d,s,size) DMA_Copy(3,s,d,/*DMA_HBLANK|*/DMA32 | ((size)>>2))
    //if(d->tile){
        //64 = 8*8
        //=gba_memcpy(d->dest_vram+varmy*0x400,d->tile + tiley*8*0x40,8*0x40);
        //72 = 8*9
        //120 = 8*15
        gba_memcpy(d->dest_vram+varmy*0x400,d->tile + tiley* d->tile_width * 0x40,(d->tile_width-0) * 0x40);
    //}
    #else
    CALL((_0x3000a00+((uint32_t)tile_copy - (uint32_t)pal_copy)),d->dest_vram+varmy*0x400,d->tile + tiley* d->tile_width * 0x40);
    #endif

    #undef gba_memcpy
}

inline static void hblank_draw_tile(hblank_draw_t *d,int varmy,int tiley){
    gba_memcpy(d->dest_vram+varmy*0x400,d->tile + tiley* d->tile_width * 0x40,(d->tile_width-0) * 0x40);
}

inline static void hblank_draw_tile_dma(hblank_draw_t *d,int varmy,int tiley){
    #define gba_memcpy(d,s,size) DMA_Copy(3,s,d,/*DMA_HBLANK|*/DMA32 | ((size)>>2))
    //if(d->tile){
        //64 = 8*8
        //gba_memcpy(d->dest_vram+varmy*0x400,d->tile + tiley*8*0x40,8*0x40);
        //72 = 8*9
        //120 = 8*15
        gba_memcpy(d->dest_vram+varmy*0x400,d->tile + tiley* d->tile_width * 0x40,(d->tile_width) * 0x40);
    //}
    #undef gba_memcpy
}

inline static void hblank_draw_tile_mix(hblank_draw_t *d,int varmy,int tiley){
    #define gba_memcpy(d,s,size) DMA_Copy(3,s,d,/*DMA_HBLANK|*/DMA32 | ((size)>>2))
    //if(d->tile){
        //64 = 8*8
        //gba_memcpy(d->dest_vram+varmy*0x400,d->tile + tiley*8*0x40,8*0x40);
        //72 = 8*9
        //120 = 8*15
        uint32_t target_addr = d->dest_vram+varmy*0x400;
        uint32_t src_addr = d->tile + tiley* d->tile_width * 0x40;
        int len = (d->tile_width-10) * 0x40;
        gba_memcpy(target_addr,src_addr,len);
    #undef gba_memcpy
        target_addr += len;
        src_addr += len;
        gba_memcpy(target_addr,src_addr,(10) * 0x40);
    //}

}

static int hblank_asm(){
    //应该复制到wram执行，性能更好
    nasm("push {r4,r5,r6,r7,lr}");
    nasm("ldr   r4,LVALUE1");
    nasm("ldrh  r3,[r4]"); //获得line
    nasm("cmp r3,#80");
    nasm("blt LQUIT");

    nasm("LQUIT:");
    nasm("pop {r4,r5,r6,r7}");
    nasm("pop {r0}");
    nasm("bx r0");
    nasm("LVALUE1:.int 0x04000006");
}

static void hblank2(){
    int line = REG_VCOUNT;
    int start = hblank_proc->start_line;
    int end = start + 80;
    strb(0x300000d,2);
    line += 1;
    if(line==hblank_proc->init_tile_line){
        //单个头像
        int y = 0;
        hblank_draw_line_t *draw_line = hblank_proc->lines + y;
        draw_line->dma[0] = draw_line->pal_src;
        draw_line->dma[1] = draw_line->pal_addr;
        draw_line->dma[2] = DMA_ENABLE | DMA32 | ((draw_line->pal_num)>>2);
        hblank_draw_tile(hblank_proc->draw+0,0,0);
        hblank_draw_tile(hblank_proc->draw+0,1,1);
    }
    if(line>=start&&line<end){
        line-= start;
        hblank_draw_line_t *draw_line = hblank_proc->lines+line;
        if(draw_line->pal_addr){
            draw_line->dma[0] = draw_line->pal_src;
            draw_line->dma[1] = draw_line->pal_addr;
            draw_line->dma[2] = DMA_ENABLE | DMA32 | ((draw_line->pal_num)>>2);
        }

        //更新tile
        int renderLine = (line+start)&0xf;
        int off = 1;
        hblank_proc->count ++;
        hblank_proc->status |= 0x10;
        //只有一个头像
        if(renderLine==8+off){
            int y = line/8;
            y += 1;
            if(y>9){
                y = 0;
            }
            hblank_draw_tile(hblank_proc->draw+0,0,y);
        }
        else if(renderLine==0+off){
            int y = line/8;
            y += 1;
            if(y>9){
                y = 0;
            }
            hblank_draw_tile(hblank_proc->draw+0,1,y);
        }

    }
}

__attribute__((optimize("O2")))
__attribute__((target("arm")))
static void hblank(){
    //垂直同步
    if(!(hblank_proc->status&3)){
        //return;
    }
    int line = REG_VCOUNT;

    line += 1;
    int start = hblank_proc->start_line;
        //80;
    int end = start + 80;
    strb(0x300000d,2);
    if(line==hblank_proc->init_tile_line){

        int y = 0;
        // //处理时不时花屏
        hblank_draw_line_t *draw_line = hblank_proc->lines + y;
        draw_line->dma[0] = draw_line->pal_src;
        draw_line->dma[1] = draw_line->pal_addr;
        draw_line->dma[2] = DMA_ENABLE | DMA32 | ((draw_line->pal_num)>>2);

        //更新一次tile
        hblank_draw_tile_wram(hblank_proc->draw+0,0,0);
        hblank_draw_tile_wram(hblank_proc->draw+0,1,1);
        hblank_draw_tile_wram(hblank_proc->draw+1,0,0);
        hblank_draw_tile_wram(hblank_proc->draw+1,1,1);
        //line = 80;
        //获得最近的调色板数据，并且更新
    }

    if(line>=start&&line<end){
        line -= start;

        //需要立即执行，否想会花屏
        hblank_draw_line_t *draw_line = hblank_proc->lines+line;

        #if 0
        //#define gba_memcpy(d,s,size) DMA_Copy(3,s,d,/*DMA_HBLANK|*/DMA32 | ((size)>>2))
        #define gba_memcpy(d,s,size) DMA_Copy(0,s,d,/*DMA_HBLANK|*/DMA32 | ((size)>>2))
        // #define gba_memcpy0(d,s,size) DMA_Copy(0,s,d,/*DMA_HBLANK|*/DMA32 | ((size)>>2))
         #define gba_memcpy3(d,s,size) DMA_Copy(3,s,d,/*DMA_HBLANK|*/DMA32 | ((size)>>2))
        //使用DMA3的话，可以实现ROM->VRAM传输，但速度不够，会花屏
        //DMA0的话，只能是内部数据传输
        #endif


        if(draw_line->pal_addr){
            nasm("XXX:");
            #if 1
            draw_line->dma[0] = draw_line->pal_src;
            draw_line->dma[1] = draw_line->pal_addr;
            draw_line->dma[2] = DMA_ENABLE | DMA32 | ((draw_line->pal_num)>>2);
            #endif
        }

        //hblank_proc->status&0x10 为安全状态,保证每次hblank时，下次hblank不进入
        //加安全锁
        //性能不够
        hblank_proc->count ++;
        hblank_proc->status |= 0x10;

        //更新tile
        int renderLine = (line+start)&0xf;
        int off = 3;
        if(renderLine==8+off){
            int y = line/8;
            y += 1;
            if(y>9){
                y = 0;
            }
            hblank_draw_tile_wram(hblank_proc->draw+0,0,y);
            hblank_draw_tile_wram(hblank_proc->draw+1,0,y);
        }
        else if(renderLine==0+off){
            int y = line/8;
            y += 1;
            if(y>9){
                y = 0;
            }
            hblank_draw_tile_wram(hblank_proc->draw+0,1,y);
            hblank_draw_tile_wram(hblank_proc->draw+1,1,y);
        }
    }
    hblank_proc->status &= ~0x10;
}

//nasm("hblank_end:");

static void update_obj_att(uint32_t r0){
    uint32_t event = r0;
    if((CALLT(0x80056AC,r0)&0x4000)){
        //
    }
    int id = geth(event+0x3e);//获得角色id
    hblank_proc->event = r0;
    //需要在80054C4 重新设置oam属性，否则对话时，口型会出现16色

    //需要判断当前角色id是否存在全色图
    if(!new_portrait[id&0xff].palbase){
        return;
    }

    if(!hblank_proc->line0){
        hblank_proc->line0 = 13;
    }
    if(!hblank_proc->line1){
        hblank_proc->line1 = 5;
    }

        uint16_t v1 = geth(event+0x30) & 0x807;
        switch (v1)
        {
        case 3:{
            //头像朝向右边
            //设置64色
            if(new_portrait[id&0xff].size[0]>64){
                str(event+0x38,oam_att_120x80);
            }
            else{
                str(event+0x38,oam_att);
            }
            //202515c
        }
        break;
        case 2:{
            if(new_portrait[id&0xff].size[0]>64){
                str(event+0x38,oam_att_120x80);
            }
            else{
                str(event+0x38,oam_att);
            }
            //朝左
        }
        break;
        default:
            break;
        }
        //继续开启HBLank
        if(!(hblank_proc->status&0x100)){
            #if 0
            #else
            if(hblank_proc->wait<=0){
                if(geth(r0+0x34)>=240){
                    strh(r0+0x34,geth(r0+0x34)-240);
                }
                irqEnable(IRQ_HBLANK);
            }
            else{
                if(geth(r0+0x34)<240){
                    strh(r0+0x34,geth(r0+0x34)+240);
                }
                hblank_proc->wait--;
            }
            #endif
        }
    //}
}

static void init(uint32_t  event);

void h800549D(uint32_t r0){
    CALLT(0x800549C,r0);
    int id = geth(r0+0x3e);//获得角色id
    //初始化头像
    hblank_proc->text_ptr = 0x3000040; //0x3000040 地址会一直往前移动，如果小于cur_text 则表示已经重新显示
    if(!hblank_proc->cur_text||(hblank_proc->text_ptr[0]<hblank_proc->cur_text)){
        //初始化所有
        hblank_proc->status = 0;
        gba_memset(hblank_proc->draw,0,sizeof(hblank_proc->draw));
        gba_memset(hblank_proc->lines,0,sizeof(hblank_proc->lines));
        gba_memset(hblank_proc->portrait,0,sizeof(hblank_proc->portrait));
    }
    hblank_proc->cur_text = hblank_proc->text_ptr[0];
    //hblank_proc->start_line = 80;
    hblank_proc->wait = 0;
    init_RAM();
    hblank_proc->start_line = geth(r0+0x36); //绘制的Y坐标
    init(r0);

    if(hblank_proc->start_line>50){
        hblank_proc->init_tile_line = 30;
    }
    else{
        hblank_proc->init_tile_line = hblank_proc->start_line + 80+20;
    }

}

void h80054C5(uint32_t r0){
    if(hblank_proc->text_ptr){
        if(hblank_proc->text_ptr[0][0]==0){
            hblank_proc->status &= ~3;
            hblank_proc->portrait[0] = 0;
            hblank_proc->portrait[1] = 0;
            hblank_proc->portrait[2] = 0;
        }
    }
    update_obj_att(r0);
    CALLT(0x80054C4,r0);
}


//如果不是直接修改0x85B8EEC数据，会导致战斗前，对话结束后，出先花屏
__attribute__ ((section(".""0x85B8EEC")))
const  uint32_t rom85B8EEC[] = {
    1, 0x80DC3F4,
    8, 0x85C29C8,
    0xE, 0,
    #if NHACK
    2, 0x800549D,   //把头像的tile数据传送到tile层
    2, 0x80054C5,   //设置精灵数据
    3, 0x80054C5,   //设置精灵数据
    #else
    2, h800549D,   //把头像的tile数据传送到tile层
    2, h80054C5,   //设置精灵数据
    3, h80054C5,   //
    //2,clear,
    #endif
    0,0,
};

static void init(uint32_t  event){
    int id = geth(event+0x3e);//获得角色id

    int offset = getb(event+0x40);
    offset *= 8;
    offset += 0x202A688;
    offset = get(offset);
    offset += 0x6010000;
    int idx = 0;
    uint8_t *tile = offset;
    if(tile==0x06016000){
        idx = 0;
    }
    else if(tile==0x06015000){
        idx = 1;
    }
    else if(tile==0x06014000){
        idx = 2;
    }
    else if(tile==0x06017000){
        idx = 3;
    }

    new_portrait_t *portrait = new_portrait + (id&0xff);
    if(!portrait->palbase){
        hblank_proc->draw[idx].tile = 0;
        hblank_proc->id[idx] = id;
        hblank_proc->status &= ~(1<<idx);
        return;
    }
    hblank_proc->status |= 1<<idx;

    uint8_t *d_tile = draw_tile;
    uint8_t **d_linepal = linepal;
    uint8_t *d_pal = draw_pal;

    hblank_proc->portrait[idx] = portrait;

    //计算当前显示的头像有多少个
    int i;
    int num = 0;
    for(i=0;i<4;i++){
        if(hblank_proc->portrait[i]){
            num++;
        }
    }

    hblank_proc->draw[idx].tile = portrait->tile[idx==0?1:0];
    hblank_proc->draw[idx].dest_vram = tile;
    hblank_proc->draw[idx].tile_width = portrait->size[0]/8;

    if(num==1||hblank_proc->show_type==show_menu){
        int off = 1;
        //当只有一个头像时
        if(hblank_proc->start_line<50){
            off = 1;
        }
        //应该关闭复制调色板
        //显示一个头像时
        hblank_proc->draw[idx].tile = portrait->tile[off]; //使用下半调色板
        hblank_proc->draw[idx].dest_vram = tile;
        int y = 0;
        //设置调色板
        gba_memset(hblank_proc->lines,0,sizeof(hblank_proc->lines));
        if(hblank_proc->start_line>50||1){
            for(y=0;y<80;y++){
                if(portrait->draw_linepal[y]){
                    hblank_proc->lines[y].pal_addr = 0x05000000+0x200 + off * PPAL_OFFSET * 2;
                    hblank_proc->lines[y].pal_src = portrait->draw_linepal[y];
                    hblank_proc->lines[y].pal_num = portrait->size[0] * 2;
                    //hblank_proc->lines[y].dma = 3;
                    hblank_proc->lines[y].dma = &(REG_DMA3SAD);
                }
            }
        }
    }
    else if(num==2){
        //显示两个及以上头像时
        //应该以调色板排序(以小到大)
        gba_memset(hblank_proc->lines,0,sizeof(hblank_proc->lines));
        int y;
        #if 0
        uint8_t *pal_ptr = 0x0600d000;
        #else
        uint8_t *pal_ptr = 0x06002000;
        #endif
        new_portrait_t **ps = hblank_proc->portrait;
        int section = 0;
        for(y=0;y<80;y++){
            int i=0;

            if(section==0){
                if(pal_ptr>0x06006000-0x200){
                    pal_ptr = 0x06017000;
                    section = 1;
                }
            }
            else if(section==1){
                if(pal_ptr>0x06018000-0x100){
                    pal_ptr = 0x06013000;
                    section = 2;
                }
            }
            else if(section==2){
                if(pal_ptr>0x06014000-0x100){
                    pal_ptr = 0x06010000;
                    section = 3;
                }
            }
            // else if(section==3){
            //     if(pal_ptr>0x06014000-0x200){
            //     }
            // }

            if(ps[0]->draw_linepal[y]&&ps[1]->draw_linepal[y]){
                //两行相同
                hblank_proc->lines[y].pal_addr = 0x05000000+0x200;
                hblank_proc->lines[y].pal_src = pal_ptr;

                gba_memcpy(pal_ptr,ps[idx==0?0:1]->draw_linepal[y],ps[0]->size[0]*2);

                pal_ptr += //(ps[0]->size[0])*2;
                    PPAL_OFFSET*2;
                gba_memcpy(pal_ptr,ps[idx==0?1:0]->draw_linepal[y],ps[0]->size[0]*2);
                pal_ptr += //(ps[1]->size[0])*2;
                    PPAL_OFFSET*2;
                hblank_proc->lines[y].pal_num = (uint32_t)pal_ptr - hblank_proc->lines[y].pal_src;
                //hblank_proc->lines[y].dma = 0;
                hblank_proc->lines[y].dma = &(REG_DMA0SAD);
                //hblank_proc->lines[y].func = 0x3000a00;
            }
            else if(ps[0]->draw_linepal[y]){
                hblank_proc->lines[y].pal_src = ps[0]->draw_linepal[y];
                hblank_proc->lines[y].pal_num = ps[0]->size[0]*2;
                #if 1
                hblank_proc->lines[y].pal_addr =  0x05000000+0x200 + (idx==0?0:1) * 2*PPAL_OFFSET; // 2*ps[0]->size[0];
                hblank_proc->lines[y].pal_src = pal_ptr;
                gba_memcpy(pal_ptr,ps[0]->draw_linepal[y],ps[0]->size[0]*2);
                pal_ptr += (ps[0]->size[0])*2;
                //hblank_proc->lines[y].dma = 0;
                #else
                hblank_proc->lines[y].pal_addr = 0x05000000+0x200;
                #endif
                //hblank_proc->lines[y].func = 0x3000a00;//+funcoffset120;
                hblank_proc->lines[y].dma = &(REG_DMA0SAD);
            }
            else if(ps[1]->draw_linepal[y]){
                hblank_proc->lines[y].pal_src = ps[1]->draw_linepal[y];
                hblank_proc->lines[y].pal_num = ps[1]->size[0]*2;
                #if 1
                hblank_proc->lines[y].pal_addr = 0x05000000+0x200 + (idx==0?1:0) * 2*PPAL_OFFSET; // 2*ps[0]->size[0];
                hblank_proc->lines[y].pal_src = pal_ptr;
                gba_memcpy(pal_ptr,ps[1]->draw_linepal[y],ps[1]->size[0]*2);
                pal_ptr += (ps[1]->size[0])*2;
                #else
                hblank_proc->lines[y].pal_addr = 0x05000000+0x200 + 2*ps[0]->size[0];
                #endif
                //hblank_proc->lines[y].dma = 0;
                hblank_proc->lines[y].dma = &(REG_DMA0SAD);
                //hblank_proc->lines[y].func = 0x3000a00 ;//+funcoffset120;
            }
        }

    }
    else{
    }
}

int _8005544_hack(uint32_t a1,uint32_t a2,int16_t a3,int16_t a4,int a5){

    if(*(uint32_t*)(0x3004920+a1*4)){
        return 0;
    }
    else{

        #if MENU_NO_SHOW
        if(hblank_proc->show_type==show_menu){
            irqSet(IRQ_HBLANK,hblank2); //全部使用本地内存
            return 0;
        }
        #endif

        uint32_t event = CALLT(0x8002BCC,0x85b8eec,5);
            //CALLT(0x8002BCC,rom85B8EEC,5);
        *(uint32_t*)(0x3004920+a1*4) = event;
        uint32_t *v = CALLT(0x800541c,a2);
        if(a5&0x2000){
            int zero = 0;
            //设置头像调色板
            CpuFastSet(&zero,
            geth(0x202A688+4+a1*8)*0x20 + 0x2022AA8,
            0x1000008
            );
            CALLT(0x8001EE4);
            //0x2022b68,对象的调色板,会实现渐变效果
        }
        else{
            //设置头像调色板
            CALLT(0x8000D68,v[2],
            (geth(0x202A688+4+a1*8)+ 0x10)*0x20 ,
            32
            );
        }

        str(event+0x2c,v);
        strb(event+0x40,a1);
        strh(event+0x3e,a2);
        strb(event+0x41,5);
        strh(event+0x34,a3);
        strh(event+0x36,a4);
        if((a5&0x1000)){
            str(event+0x44,0);
            str(event+0x48,0);
        }
        else{
            //str(event+0x44,CALLT(0x8002bcc,0x85b8fe4,event));
            //str(event+0x48,CALLT(0x8002bcc,0x85b8ffc,event));
        }
        str(event+0x30,~a5);
        CALLT(0x8005678,event,a5,0); //设置精灵属性的数据
        //gba_memcpy(hblank_proc->exec,hblank,(uint32_t)hblank_end - (uint32_t)hblank + 0x500);


        if(hblank_proc->show_type==show_menu){
            init_RAM();
            irqSet(IRQ_HBLANK,hblank); //全部使用ROM内存
        }
        else{
            init_RAM();
            //irqSet(IRQ_HBLANK,_0x3000b30);
            irqSet(IRQ_HBLANK,hblank);
        }
        //irqEnable(IRQ_HBLANK);

        return event;
    }
}

int _8005548_r7(){
    nasm("mov r0,r0")
    nasm("JMP8005548_r7:")
    nasm("ldr r4,[sp,#4*4]")
    nasm("mov lr,r4")
    nasm("pop {r4,r5,r6,r7}")
    nasm("add sp,sp,#4")
    nasm("b _8005544_hack");
}


///////////////////////////////////////
//处理对话关闭的头像
void JMP8005E3C_r2(uint32_t r0){

    #if 1
    //修改

    int offset = getb(r0+0x40);
    offset *= 8;
    offset += 0x202A688;
    offset = get(offset);
    offset += 0x6010000;
    int tile = offset;
    int idx;
    if(tile==0x06016000){
        idx = 0;
    }
    else if(tile==0x06015000){
        idx = 1;
    }
    else if(tile==0x06014000){
        idx = 2;
    }
    else if(tile==0x06017000){
        idx = 3;
    }

    hblank_proc->status &= ~(1<<idx); //关闭一个显示头像
    hblank_proc->draw[idx].tile = 0; //关闭绘制
    hblank_proc->draw[idx].dest_vram = 0;
    hblank_proc->draw[idx].tile_width = 0x10;
    hblank_proc->portrait[idx] = 0;

    //
    hblank_proc->event = r0;
    strh(r0+0x34,0x160); //关闭显示，防止花屏


    if(idx==0&&hblank_proc->portrait[1]
    ){
        //需要重新初始化绘制线
        idx = idx==0?1:0;
        new_portrait_t *portrait = hblank_proc->portrait[idx];
        hblank_proc->draw[idx].tile = portrait->tile[1]; //使用下半调色板
        //hblank_proc->draw[idx].dest_vram = tile; //不更改目标VRAM
        int y = 0;
        //设置调色板
        gba_memset(hblank_proc->lines,0,sizeof(hblank_proc->lines));
        for(y=0;y<80;y++){
            if(portrait->draw_linepal[y]){
                hblank_proc->lines[y].pal_addr = 0x05000000+0x200 + PPAL_OFFSET*2;
                hblank_proc->lines[y].pal_src = portrait->draw_linepal[y];
                hblank_proc->lines[y].pal_num = portrait->size[0] * 2;
                //hblank_proc->lines[y].dma = 3;
                hblank_proc->lines[y].dma = &(REG_DMA3SAD);
            }
        }
    }

    #endif

    CALLT(0x800541C,geth(r0+0x3e)); //没有任何作用?
    CALLT(0x80139A8,
    geth(8*getb(r0+0x40)+0x202a68c)+0x10,
    12,r0);
    CALLT(0x8005DDC,r0);
}

///////////////////////////
//很多时候，这个函数
//这个函数处理花屏，因为wram调色板数据还在复制到palram中
void JMP80010FC_r1(){
    uint32_t v = getb(0x300000C);
    if(v&1){
        //图层数据
        CALLT(0x80D636C,0x2022CA8,get(0x2024CA8),512);
    }
    if(v&2){
        CALLT(0x80D636C,0x20234A8,get(0x2024CAC),512);
    }
    if(v&4){
        CALLT(0x80D636C,0x2023CA8,get(0x2024CB0),512);
    }
    if(v&8){
        CALLT(0x80D636C,0x20244A8,get(0x2024CB4),512);
    }
    strb(0x300000C,0);
    int status = getb(0x300000D);
    if(status){
        strb(0x300000D,0);
        int v2 = (int8_t)getb(0x3003088);
        if(v2){
            if(v2<=0){
                CALLT(0x80010A0,v2);
            }
            else{
                CALLT(0x8001044,v2);
            }
        }
        else{
            int size = 0;
            if(status==2){
                size = 0x300>>2;
            }
            else{
                size = 0x100;
            }
            CALLT(0x80D636C,0x20228A8, 0x5000000, size); //测试只复制一半数据
        }
    }
}


#if 1

/*
对话自然结束时，会执行
8006BAA
[85B9108+4] 存储着 对话初始化的函数
*/
void H85B910C(){
    hblank_proc->show_type = show_text;
    CALLT(0x8006B04); //执行原先的程序
}

HACK_DATA(0x85B910C)
const u32 _0x85B910C = H85B910C;


//下面是针对菜单初始化时的程序
//角色面板
u32 hack8022BA0(){
    //设置显示头像的状态
    hblank_proc->show_type = show_menu;
    return CALLT(0x804F970,0x85C56AC); //必须执行这个指令
}

void _8022BA0(){
    nasm("mov r0,r0");
    nasm("JMP8022BA0_r2:");
    nasm("push {r4,r5,lr}");
    nasm("sub sp,sp,#4");
    nasm("bl hack8022BA0");
    nasm("ldr r1,=0x8022BAA+1");
    nasm("bx r1");
}

#endif
